package org.chickymate.scrumboard.client.presenter;

import org.chickymate.scrumboard.client.model.Board;
import org.chickymate.scrumboard.client.model.LoginInfo;
import org.chickymate.scrumboard.client.service.BoardServiceAsync;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class CreateBoardPresenter implements Presenter {

	public interface Display{
		HasClickHandlers getSaveButton();
		HasClickHandlers getCancelButton();
		HasValue<String> getBoardName();
		Widget asWidget();		
	}
	
	private final BoardServiceAsync boardService;
	private final HandlerManager eventBus;
	private final Display display;
	private final LoginInfo loginInfo;
		
	public CreateBoardPresenter(BoardServiceAsync boardService,
			HandlerManager eventBus, Display display,LoginInfo loginInfo) {
		super();
		this.boardService = boardService;
		this.eventBus = eventBus;
		this.display = display;
		this.loginInfo = loginInfo;
		bind();
	}

	private void bind() {
		this.display.getSaveButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				doSave();
			}
			
		});		
	}
	
	private void doSave() {
		String mailAddress = loginInfo.getEmailAddress();
		boardService.createNewBoard(mailAddress, display.getBoardName().getValue(), new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stu				
			}

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				
			}
		});		
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}
}
